package com.shen.accountbook.module.splash;

import android.content.Context;

import com.google.gson.Gson;
import com.shen.accountbook.bean.XSortBean;
import com.shen.accountbook.bean.XSortBean.XFirstSortBean;
import com.shen.accountbook.bean.XSortBean.XFirstSortBean.XSecondSortBean;
import com.shen.accountbook.bean.XSortBean.XFirstSortBean.XSecondSortBean.XThirdSortBean;
import com.shen.accountbook.bean.temp.biz.FirstSortBeanBiz;
import com.shen.accountbook.bean.temp.biz.SecondSortBeanBiz;
import com.shen.accountbook.bean.temp.biz.ThirdSortBeanBiz;
import com.shen.accountbook.module.base.BasePresenter;
import com.shen.accountbook.utils.FilesUtils;
import com.shen.accountbook.utils.L;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * author:  shen
 * date:    2018/6/27
 */
public class SplashScreenPresenterImpl extends BasePresenter<SplashScreenView, SplashScreenModelImpl> {


    public SplashScreenPresenterImpl(Context context, SplashScreenView mvpView) {
        super(context, mvpView);
    }

    @Override
    protected SplashScreenModelImpl createModel() {
        return new SplashScreenModelImpl();
    }


    /**
     * 倒计时
     */
    public void countDown() {
        final long count = 3;
        Observable.interval(0, 1, TimeUnit.SECONDS) // 设置0延迟，每隔一秒发送一条数据
                .take((int) (count + 1))                            // 设置总共发送的次数
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;       // long 值是从小到大，倒计时需要将值倒置
                    }

                })
                .subscribeOn(Schedulers.io())            // 上游
                .observeOn(AndroidSchedulers.mainThread())// 下游
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Object o) {
                        mvpView.setTvText(o + "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        mvpView.goHome();
                    }

                    @Override
                    public void onComplete() {
                        mvpView.goHome();
                    }
                });
    }

    protected void initDataBase(){
        Gson gson = new Gson();
        String json = FilesUtils.getFromAssets(mContext, "sort.json");
        XSortBean xSortBean = gson.fromJson(json, XSortBean.class);

        if(xSortBean != null){
            insertFirst(xSortBean.getXFirstSort());
        }
    }




    private void insertFirst(List<XFirstSortBean> beanList){
        if(beanList != null) {
            for (XFirstSortBean xFirstSortBean : beanList) {
                FirstSortBeanBiz biz = new FirstSortBeanBiz();
                long rawId = biz.insert(xFirstSortBean.getId(), xFirstSortBean.getName());

                L.i(rawId + "----------" +xFirstSortBean.toString());
                insertSecond(xFirstSortBean, xFirstSortBean.getXSecondSort());
            }
        }
    }


    private void insertSecond(XFirstSortBean xFirstSortBean, List<XSecondSortBean> beanList){
        if(beanList != null) {
            for (XSecondSortBean xSecondSortBean : beanList) {
                SecondSortBeanBiz biz = new SecondSortBeanBiz();
                long rawId = biz.insert(xFirstSortBean.getId(), xSecondSortBean.getId(), xSecondSortBean.getName());

                L.i(rawId + "----------" +xSecondSortBean.toString());
                insertThird(xFirstSortBean, xSecondSortBean, xSecondSortBean.getXThirdSort());
            }
        }
    }


    private void insertThird(XFirstSortBean xFirstSortBean, XSecondSortBean xSecondSortBean, List<XThirdSortBean> beanList){
        if(beanList != null) {
            for (XThirdSortBean xThirdSortBean : beanList) {
                ThirdSortBeanBiz biz = new ThirdSortBeanBiz();
                long rawId = biz.insert(xFirstSortBean.getId(),
                        xSecondSortBean.getId(),
                        xThirdSortBean.getId(),
                        xThirdSortBean.getName(), "");

                L.i(rawId + "----------" +xThirdSortBean.toString());

            }
        }
    }
}
